//
//  BangDanCell.m
//  danpin
//
//  Created by chuangjia on 15/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "BangDanCell.h"
#import "ZUIImageView.h"
@interface BangDanCell()
{
    
}
@property (nonatomic, strong) UIView *bgView;
@property(nonatomic,strong)RTLabel *skuNameLabel;
@property(nonatomic,strong)RTLabel *skuNameContent;
@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)RTLabel *cateLabel;
@property(nonatomic,strong)UIButton *recommendBtn;
@property (nonatomic, strong) UIView *commentView;
@property(nonatomic,strong)UIImageView *lineView;
@end


@implementation BangDanCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        //
        _skuNameLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _skuNameLabel.backgroundColor = [UIColor clearColor];
        _skuNameLabel.textColor = [UIColor darkGrayColor];
        _skuNameLabel.textAlignment = NSTextAlignmentLeft;
        _skuNameLabel.font = FONT(16);
        _skuNameLabel.lineSpacing = LSpacing;

        [_bgView addSubview:_skuNameLabel];
        
        //
        _skuNameContent =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _skuNameContent.backgroundColor = [UIColor clearColor];
        _skuNameContent.textColor = [UIColor darkGrayColor];
        _skuNameContent.textAlignment = NSTextAlignmentLeft;
        _skuNameContent.font = FONT(14);
        _skuNameContent.lineSpacing =LSpacing;
        [_bgView addSubview:_skuNameContent];
        //
        _iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _iconView.image =[UIImage imageNamed:@"bangdan.png"];
        [self.contentView addSubview:_iconView];
        //
        _cateLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _cateLabel.backgroundColor = [UIColor clearColor];
        _cateLabel.textColor = UIColorFromRGB(0x999999);
        _cateLabel.textAlignment = NSTextAlignmentLeft;
        _cateLabel.font = FONT(14);
        _cateLabel.lineSpacing =LSpacing;
        [_bgView addSubview:_cateLabel];
      //
        _commentView = [[UIView alloc] initWithFrame:CGRectZero];
        _commentView.backgroundColor =[UIColor clearColor];
        [_bgView addSubview:_commentView];
      
        _recommendBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _recommendBtn.backgroundColor =[UIColor clearColor];
        _recommendBtn.layer.borderColor=UIColorFromRGB(0xcccccc).CGColor;
        _recommendBtn.layer.borderWidth =0.5;
        _recommendBtn.layer.cornerRadius = 2.5;
        [_recommendBtn setTitleColor:UIColorFromRGB(0x285790) forState:UIControlStateNormal];
        _recommendBtn.titleLabel.font = FONT(14);
        [_recommendBtn addTarget:self action:@selector(bangdanAction) forControlEvents:UIControlEventTouchUpInside];
        [_recommendBtn setTitle:@"推荐到此榜单" forState:UIControlStateNormal];
        [_bgView addSubview:_recommendBtn];
     
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    
    [self.skuNameLabel removeFromSuperview];
    self.skuNameLabel=nil;
    [self.skuNameContent removeFromSuperview];
    self.skuNameContent =nil;
    self.iconView.image=nil;
    [self.iconView removeFromSuperview];
    self.iconView=nil;
    [self.cateLabel removeFromSuperview];
    self.cateLabel =nil;
    [self.recommendBtn removeFromSuperview];
    self.recommendBtn=nil;
    [self.commentView removeFromSuperview];
    self.commentView=nil;
    [self.lineView removeFromSuperview];
    self.lineView=nil;
    [self.bgView removeFromSuperview];
    self.bgView=nil;
    
//    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(0,self.bounds.size.height-1, DEVICE_WIDTH, 1);
    self.recommendBtn.frame =CGRectMake(DEVICE_WIDTH-120-PHOTO_FRAME_WIDTH,self.bounds.size.height-PHOTO_FRAME_WIDTH-35-PHOTO_TIME_PADDING, 120, 35);
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other =other;
    self.key =key;
    self.data =model;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key = key;
    self.data =model;
    [self showInfo:model indexPath:indexPath];
}
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    self.indexPath=indexPath;
    float fTop = 0;
    fTop += PHOTO_FRAME_WIDTH*2;
   
    //
    NSString *skuname =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (skuname.length>0) {
        [_skuNameLabel setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [_skuNameLabel optimumSize];
        CGFloat hh =optimumSize.height;
        if (hh<20) {
            hh=20;
        }
        _skuNameLabel.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, hh);
     
        fTop = fTop+hh+PHOTO_FRAME_WIDTH;
    }
    _iconView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, 32, 16);
    
    NSString *ruser =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"recommend_user"]] replaceNullString];
    if (ruser.length==0 || [ruser isEqualToString:@"(null)"])
    {
        ruser =@"0";
    }
    NSString *puser =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"push_user"]] replaceNullString];
    if (puser.length==0 || [puser isEqualToString:@"(null)"])
    {
        puser =@"0";
    }
    
    NSString *recommend_user =[NSString stringWithFormat:@"%@个推荐 / %@个人参与", ruser,puser];
    _cateLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH*2+32,fTop,DEVICE_WIDTH - 32-PHOTO_FRAME_WIDTH*3, 20);
    _cateLabel.text =recommend_user;
    //
    fTop = fTop+PHOTO_FRAME_WIDTH*3;
    //
    NSString *text =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]] replaceNullString];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"“%@”",text];
        [_skuNameContent setText:text];
        CGSize optimumSize1 = [_skuNameContent optimumSize];
        _skuNameContent.frame = CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, optimumSize1.height);
        fTop = fTop+optimumSize1.height+PHOTO_FRAME_WIDTH;
    }
    //
    id comments = [dict objectForKey:@"detailPicURL"];
    if (comments && [comments isKindOfClass:[NSArray class]]&&[comments count]>0) {
        NSMutableArray *cmtArray =[NSMutableArray arrayWithArray:comments];
        int cmtCount =(int)[cmtArray count];
        if(cmtCount>=9)
        {
            cmtCount = 9;
        }
        if (cmtCount>0) {
            int totalloc = 3;
            CGFloat appvieww=((DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)/3);
            CGFloat margin=(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2-totalloc*appvieww)/(totalloc+1);
            for (int i =0 ; i<cmtCount; i++) {
                int row=i/totalloc;//行号
                //1/3=0,2/3=0,3/3=1;
                int loc=i%totalloc;//列号
                CGFloat appviewx=margin+(margin+appvieww)*loc;
                CGFloat appviewy=margin+(margin+appvieww)*row;
                
                NSDictionary *cDict = [cmtArray objectAtIndex:i];
                NSString *urlstr =[NSString stringWithFormat:@"%@",[cDict objectForKey:@"url"]];
                ZUIImageView *imageView =[[ZUIImageView alloc] initWithFrame:CGRectZero];
                imageView.frame = CGRectMake(appviewx,appviewy, appvieww ,appvieww);
                imageView.backgroundColor =[UIColor whiteColor];
                imageView.imageUrl = urlstr;
                imageView.placeholderImage =[UIImage imageNamed:@"image.png"];
                [_commentView addSubview:imageView];
                imageView=nil;
            }
            _commentView.frame =CGRectMake(PHOTO_FRAME_WIDTH,fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, (DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)/3);
//            fTop =fTop +_commentView.frame.size.height+PHOTO_FRAME_WIDTH;
        }
    }else
    {
//        fTop =fTop +PHOTO_TIME_PADDING;
    }
    
    
    [self layoutSubviews];
}


/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    NSString *skuname =[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]];
    CGFloat hight =0.0;
    if (skuname.length>0) {
        RTLabel *text1Label =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        text1Label.font = FONT(16);
        text1Label.lineSpacing = LSpacing;
        [text1Label setText:[NSString stringWithFormat:@"%@",skuname]];
        CGSize optimumSize = [text1Label optimumSize];
        text1Label=nil;
        CGFloat hh1 =optimumSize.height;
        if (hh1<20) {
            hh1=20;
        }
        hight = hight +hh1+PHOTO_FRAME_WIDTH;
    }
    //
    NSString *text =[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]];
    if (text.length>0) {
        text =[NSString stringWithFormat:@"“%@”",text];
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:text];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        CGFloat hh1 =optimumSize1.height;
        hight =hight + hh1+PHOTO_FRAME_WIDTH*2+PHOTO_FRAME_WIDTH;
    }
    id comments = [dict objectForKey:@"detailPicURL"];
    if (comments && [comments isKindOfClass:[NSArray class]]) {
        NSMutableArray *cmtArray =[NSMutableArray arrayWithArray:comments];
        int cmtCount =(int)[cmtArray count];
        if(cmtCount>=9)
        {
            cmtCount = 9;
        }
        if (cmtCount>0) {
            CGFloat appvieww=((DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2)/3);
            int rowc = cmtCount%3==0?cmtCount/3:cmtCount/3+1;
            hight =hight + rowc*appvieww +PHOTO_FRAME_WIDTH;
        }
    }
    return hight+35+PHOTO_FRAME_WIDTH*5;
    
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)bangdanAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_recommendTobangdan,self,self.data,self.key,self.indexPath);
    }
}
@end
